from mxupy import IM, EntityXControl
import OBEvolutionAI as obe


class SchoolControl(EntityXControl):

    class Meta:
        model_class = obe.School
    def get_list(self, select=None, where=None, order_by=None, group_by=None, having=None, limit=100, offset=None, to_dict=False, recurse=False, backrefs=False, max_depth=1):
        """ 获取学校列表 """
        return super().get_list(select=select, where=where, order_by=order_by, group_by=group_by, having=having, limit=limit, offset=offset, to_dict=to_dict, recurse=recurse, backrefs=backrefs, max_depth=max_depth)

    def delete(self, model, recursive=True):
        """ 删除学校 """
        school_id = model.get('schoolId') if isinstance(model, dict) else model.schoolId
        
        if school_id == 1:
            return IM().set_error("不能删除默认学校!")

        if (im := obe.CollegeControl.inst().exists({"schoolId": school_id})).error:
            return im
        if im.data :
            return IM().set_error("该学校下有学院，不能删除！")

        return super().delete(model, recursive)
    